;;; lists-tests.el --- Tests for lists.rs

;;; Code:

(require 'ert)

;; Test handling of cyclic and dotted lists.

(defun cyc1 (a)
  (let ((ls (make-list 10 a)))
    (nconc ls ls)
    ls))

(defun cyc2 (a b)
  (let ((ls1 (make-list 10 a))
        (ls2 (make-list 1000 b)))
    (nconc ls2 ls2)
    (nconc ls1 ls2)
    ls1))

(defun dot1 (a)
  (let ((ls (make-list 10 a)))
    (nconc ls 'tail)
    ls))

(defun dot2 (a b)
  (let ((ls1 (make-list 10 a))
        (ls2 (make-list 10 b)))
    (nconc ls1 ls2)
    (nconc ls2 'tail)
    ls1))

(ert-deftest lists-test--nth-base ()
  (should (eq (nth 0 '(a b c)) 'a))
  (should (eq (nth 1 '(a b c)) 'b))
  (should (eq (nth 2 '(a b c)) 'c))
  (should (eq (nth 3 '(a b c)) nil))
  (should (eq (nth -1 '(a b c)) 'a))
  )

(ert-deftest lists-test--nthcdr-base ()
  (should (eq (nth 0 '(a b c)) 'a))
  (should (eq (nth 1 '(a b c)) 'b))
  (should (eq (nth 2 '(a b c)) 'c))
  (should (eq (nth 3 '(a b c)) nil))
  (should (eq (nth -1 '(a b c)) 'a))
  )

(ert-deftest plist-get/odd-number-of-elements ()
  "Test that `plist-get' doesn't signal an error on degenerate plists."
  (should-not (plist-get '(:foo 1 :bar) :bar)))

(ert-deftest lax-plist-get/odd-number-of-elements ()
  "Check for https://debbugs.gnu.org/cgi/bugreport.cgi?bug=27726."
  (should (equal (should-error (lax-plist-get '(:foo 1 :bar) :bar)
                               :type 'wrong-type-argument)
                 '(wrong-type-argument plistp (:foo 1 :bar)))))

(ert-deftest plist-put/odd-number-of-elements ()
  "Check for https://debbugs.gnu.org/cgi/bugreport.cgi?bug=27726."
  (should (equal (should-error (plist-put '(:foo 1 :bar) :zot 2)
                               :type 'wrong-type-argument)
                 '(wrong-type-argument plistp (:foo 1 :bar)))))

(ert-deftest lax-plist-put/odd-number-of-elements ()
  "Check for https://debbugs.gnu.org/cgi/bugreport.cgi?bug=27726."
  (should (equal (should-error (lax-plist-put '(:foo 1 :bar) :zot 2)
                               :type 'wrong-type-argument)
                 '(wrong-type-argument plistp (:foo 1 :bar)))))

(ert-deftest plist-member/improper-list ()
  "Check for https://debbugs.gnu.org/cgi/bugreport.cgi?bug=27726."
  (should (equal (should-error (plist-member '(:foo 1 . :bar) :qux)
                               :type 'wrong-type-argument)
                 '(wrong-type-argument plistp (:foo 1 . :bar)))))

(ert-deftest test-cycle-plist-get ()
  (let ((c1 (cyc1 1))
        (c2 (cyc2 1 2))
        (d1 (dot1 1))
        (d2 (dot2 1 2)))
    (should (plist-get c1 1))
    (should (plist-get c2 1))
    (should (plist-get d1 1))
    (should (plist-get d2 1))
    (should-not (plist-get c1 2))
    (should (plist-get c2 2))
    (should-not (plist-get d1 2))
    (should (plist-get d2 2))
    (should-not (plist-get c1 3))
    (should-not (plist-get c2 3))
    (should-not (plist-get d1 3))
    (should-not (plist-get d2 3))))

(ert-deftest test-cycle-lax-plist-get ()
  (let ((c1 (cyc1 1))
        (c2 (cyc2 1 2))
        (d1 (dot1 1))
        (d2 (dot2 1 2)))
    (should (lax-plist-get c1 1))
    (should (lax-plist-get c2 1))
    (should (lax-plist-get d1 1))
    (should (lax-plist-get d2 1))
    (should-error (lax-plist-get c1 2) :type 'circular-list)
    (should (lax-plist-get c2 2))
    (should-error (lax-plist-get d1 2) :type 'wrong-type-argument)
    (should (lax-plist-get d2 2))
    (should-error (lax-plist-get c1 3) :type 'circular-list)
    (should-error (lax-plist-get c2 3) :type 'circular-list)
    (should-error (lax-plist-get d1 3) :type 'wrong-type-argument)
    (should-error (lax-plist-get d2 3) :type 'wrong-type-argument)))

(ert-deftest test-cycle-plist-member ()
  (let ((c1 (cyc1 1))
        (c2 (cyc2 1 2))
        (d1 (dot1 1))
        (d2 (dot2 1 2)))
    (should (plist-member c1 1))
    (should (plist-member c2 1))
    (should (plist-member d1 1))
    (should (plist-member d2 1))
    (should-error (plist-member c1 2) :type 'circular-list)
    (should (plist-member c2 2))
    (should-error (plist-member d1 2) :type 'wrong-type-argument)
    (should (plist-member d2 2))
    (should-error (plist-member c1 3) :type 'circular-list)
    (should-error (plist-member c2 3) :type 'circular-list)
    (should-error (plist-member d1 3) :type 'wrong-type-argument)
    (should-error (plist-member d2 3) :type 'wrong-type-argument)))

(ert-deftest test-cycle-plist-put ()
  (let ((c1 (cyc1 1))
        (c2 (cyc2 1 2))
        (d1 (dot1 1))
        (d2 (dot2 1 2)))
    (should (plist-put c1 1 1))
    (should (plist-put c2 1 1))
    (should (plist-put d1 1 1))
    (should (plist-put d2 1 1))
    (should-error (plist-put c1 2 2) :type 'circular-list)
    (should (plist-put c2 2 2))
    (should-error (plist-put d1 2 2) :type 'wrong-type-argument)
    (should (plist-put d2 2 2))
    (should-error (plist-put c1 3 3) :type 'circular-list)
    (should-error (plist-put c2 3 3) :type 'circular-list)
    (should-error (plist-put d1 3 3) :type 'wrong-type-argument)
    (should-error (plist-put d2 3 3) :type 'wrong-type-argument)))

(ert-deftest test-cycle-lax-plist-put ()
  (let ((c1 (cyc1 1))
        (c2 (cyc2 1 2))
        (d1 (dot1 1))
        (d2 (dot2 1 2)))
    (should (lax-plist-put c1 1 1))
    (should (lax-plist-put c2 1 1))
    (should (lax-plist-put d1 1 1))
    (should (lax-plist-put d2 1 1))
    (should-error (lax-plist-put c1 2 2) :type 'circular-list)
    (should (lax-plist-put c2 2 2))
    (should-error (lax-plist-put d1 2 2) :type 'wrong-type-argument)
    (should (lax-plist-put d2 2 2))
    (should-error (lax-plist-put c1 3 3) :type 'circular-list)
    (should-error (lax-plist-put c2 3 3) :type 'circular-list)
    (should-error (lax-plist-put d1 3 3) :type 'wrong-type-argument)
    (should-error (lax-plist-put d2 3 3) :type 'wrong-type-argument)))

(provide 'rust-lists-tests)
;;; lists-tests.el ends here
